<template>
  <div v-loading="loading">
    <div class="search-box">
      <div class="search-form">
        <el-form ref="searchForm" :inline="true" :model="searchForm">
          <el-form-item label="业务名称" prop="procDefKey">
            <el-select v-model="searchForm.procDefKey" clearable style="width:180px;">
              <el-option
                v-for="item in procDefKeyOptions"
                :key="item.id"
                :label="item.name"
                :value="item.id"
              />
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="el-icon-search" @click="onSearch">查询</el-button>
            <el-button icon="el-icon-remove-outline" title="重置" @click="resetSearchForm" />
          </el-form-item>
        </el-form>
      </div>
    </div>

    <el-table
      ref="multipleTable"
      stripe
      :data="tableData"
      border
      class="table"
    >
      <el-table-column prop="procDefKey" label="业务名称">
        <template slot-scope="scope">
          {{ scope.row.procDefKey|toProcessName }}
        </template>
      </el-table-column>
      <el-table-column prop="procName" label="申请标题" />
      <el-table-column prop="applicant" label="申请人" />
      <el-table-column prop="applyTime" label="申请时间" width="170">
        <template slot-scope="scope">
          <span>{{ scope.row.applyTime | parseTime("{y}-{m}-{d} {h}:{i}") }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="name" label="任务名称" />
      <el-table-column prop="ownerName" label="所有者" />
      <el-table-column prop="createTime" label="任务开始时间" width="170">
        <template slot-scope="scope">
          <span>{{ scope.row.createTime | parseTime("{y}-{m}-{d} {h}:{i}") }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="120" align="left">
        <template slot-scope="scope">
          <el-button v-if="scope.row.taskDefKey=='NotifyTask'" type="text" icon="el-icon-edit" @click="handleNotify(scope.row)">查看</el-button>
          <el-button v-else-if="scope.row.taskDefKey=='trainingApply'||scope.row.taskDefKey=='socialApply'" type="text" icon="el-icon-edit" @click="handleReapply(scope.row)">重新申请</el-button>
          <el-button v-else type="text" icon="el-icon-edit" @click="handleApprove(scope.row)">审批</el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination :total="total" :page.sync="curPage" :limit.sync="limit" @pagination="getList" />
    <!--表单组件-->
    <awardForm ref="awardForm" />
    <leaveForm ref="leaveForm" />
    <trainingForm ref="trainingForm" />
    <trainingApply ref="trainingApply" />
    <socialJobForm ref="socialJobForm" />
    <socialJobApply ref="socialJobApply" />
    <publishForm ref="publishForm" />
    <publishAudit ref="publishAudit" />
    <trainingShow ref="trainingShow2" />
  </div>
</template>

<script>
import request from '@/utils/request'
import Pagination from '@/components/Pagination'
import { processKeys } from '@/api/basedata'
import leaveForm from '../../edu/leave/approve/form'
import awardForm from '../../edu/award/approve/form'
import publishForm from '../../edu/publish/approve/form'
import publishAudit from '../../edu/publish/approve/audit'
import trainingForm from '../../edu/training/approve/form'
import socialJobForm from '../../edu/socialjob/approve/form'
import trainingApply from '../../edu/training/apply/form'
import trainingShow from '../../edu/training/list/show'
import socialJobApply from '../../edu/socialjob/apply/form'
export default {
  name: 'ProcessRunning',
  components: { Pagination, leaveForm, awardForm, trainingForm, trainingApply, socialJobForm, socialJobApply, publishForm, publishAudit, trainingShow },
  data() {
    return {
      curPage: 1,
      limit: 5,
      total: 0,
      loading: true,
      downloadLoading: false,
      orderByClause: null,
      procDefKeyOptions: processKeys,
      tableData: [],
      searchForm: {
        procDefKey: ''
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    // 获取数据
    getList() {
      this.loading = true
      const param = {}
      const queryPage = {}
      queryPage.limit = this.limit
      queryPage.offset = this.limit * (this.curPage - 1)
      param.page = queryPage
      if (this.orderByClause && this.orderByClause.trim() !== '') {
        param.orderByClause = this.orderByClause
      }
      this.copyNotBlankField(this.searchForm, param)
      request({ url: '/task/findRuntimeTasks', method: 'post', data: param })
        .then(res => {
          this.loading = false
          this.tableData = res.rows
          this.total = res.total
          this.curPage = res.pageNumber
        })
        .catch(e => {
          this.loading = false
          this.$message.error('查询出错')
        })
    },
    onSearch() {
      this.getList()
    },
    resetSearchForm() {
      this.$refs['searchForm'].resetFields()
    },
    handleApprove(row) {
      if (row.procDefKey === 'award') {
        this.$refs.awardForm.show(row)
      } else if (row.procDefKey === 'leave') {
        this.$refs.leaveForm.show(row)
      } else if (row.procDefKey === 'training') {
        this.$refs.trainingForm.show(row)
      } else if (row.procDefKey === 'socialJob') {
        this.$refs.socialJobForm.show(row)
      } else if (row.procDefKey === 'publish') {
        if (row.taskDefKey === 'publishAprove') {
          this.$refs.publishForm.show(row)
        } else {
          this.$refs.publishAudit.show(row)
        }
      }
    },
    handleReapply(row) {
      if (row.procDefKey === 'training') {
        this.$refs.trainingApply.reApply(row.businessKey)
      } else if (row.procDefKey === 'socialJob') {
        this.$refs.socialJobApply.reApply(row.businessKey)
      }
    },
    handleNotify(row) {
      if (row.procDefKey === 'training') {
        this.$refs.trainingShow2.show(row.businessKey)
      }
      // completeNotifyTask
      request({ url: '/task/completeNotifyTask', method: 'get', params: { id: row.taskId }})
    }
  }
}
</script>
